/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package struts.actions;

import business.Document;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Eduardo
 */
public class UserAction {

    public static final int NPAGES = 10;

    private AdminAction adminAction;
    private String searchText;
    private List searchResult;
    private List searchResultPaginate;
    private long searchTime;


    public UserAction(){}

    public void search()
    {
        long t0 = System.currentTimeMillis();

        searchResult = adminAction.search(searchText);

        long tf = System.currentTimeMillis();        
        searchTime = tf - t0;

        //Paginamos
        searchResultPaginate = new ArrayList();
        if(searchResult.size() > 0)
        {
            for(int i=0; i<NPAGES && i<searchResult.size(); i++)
                searchResultPaginate.add(searchResult.get(i));
        }
    }   

    public Document getDocumentById(int idDoc)
    {
        return this.adminAction.getSm().getDocumentById(idDoc);
    }


    public String capitalizeSentence(String s)
    {
        String[] l = s.split(" ", 2);
        l[0] = l[0].substring(0, 1).toUpperCase() + l[0].substring(1);
        if(l.length == 2)
            return l[0] + " " + l[1];
        else
            return l[0];
    }

    public boolean isEmpty()
    {
        return adminAction.isEmpty();
    }

    public String getFileName(int fileId)
    {
        return adminAction.getFileName(fileId);
    }

    public String getDocumentText(Document doc)
    {
        String resume = "... ";
        String[] text = adminAction.getDocumentText(doc).split(" ");

        String[] words = this.getCleanQuery().split(" ");
        for(String s : words)
        {           
            for(int i=0; i<text.length; i++)
            {
                if(text[i].contains(s))
                {                    
                    if(i > 3) resume += text[i-3] + " ";
                    if(i > 2) resume += text[i-2] + " ";
                    if(i > 1)resume += text[i-1] + " ";
                    resume += text[i] + " ";
                    if(i < text.length -1) resume += text[i+1] + " ";
                    if(i < text.length -2) resume += text[i+2] + " ";
                    if(i < text.length - 3) resume += text[i+3] + " ";
                    resume += " ...";
                }
            }
        }

        return resume;
    }

    public String getCleanQuery()
    {
        return adminAction.getCleanQuery(searchText);
    }   
    




    public AdminAction getAdminAction() {
        return adminAction;
    }

    public void setAdminAction(AdminAction adminAction) {
        this.adminAction = adminAction;
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }

    public List getSearchResult() {
        return searchResult;
    }

    public void setSearchResult(List searchResult) {
        this.searchResult = searchResult;
    }

    public long getSearchTime() {
        return searchTime;
    }

    public void setSearchTime(long searchTime) {
        this.searchTime = searchTime;
    }

    public List getSearchResultPaginate(int page) {

        searchResultPaginate = new ArrayList();
        for(int i=page*NPAGES; i<(page+1)*NPAGES && i<searchResult.size(); i++)
            searchResultPaginate.add(searchResult.get(i));
        return searchResultPaginate;
    }

    public void setSearchResultPaginate(List searchResultPaginate) {
        this.searchResultPaginate = searchResultPaginate;
    }


}
